package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseSaleAttr;
import com.atguigu.gmall.product.entity.BaseTrademark;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SaveSpuInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "spu")
@RestController
@RequestMapping("/admin/product")
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    BaseSaleAttrService baseSaleAttrService;
    @Autowired
    BaseTrademarkService baseTrademarkService;

    //http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61  baseSaleAttrList
    @ApiOperation("获取spu分页列表")
    @GetMapping("/{page}/{limit}")
    public Result getProduct(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestParam("category3Id") Long category3Id){
//        Page<SpuInfo> result = spuInfoService.getPage(page,limit,category3Id);
        //优化代码
        Page<SpuInfo> result = spuInfoService.lambdaQuery().eq(SpuInfo::getCategory3Id, category3Id).page(new Page<>(page, limit));
        return Result.ok(result);
    }

    //http://api.gmall.com/admin/product/baseSaleAttrList  baseSaleAttrList
    @ApiOperation("获取销售属性")
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        List<BaseSaleAttr> list = baseSaleAttrService.list();
        return Result.ok(list);
    }

    //http://api.gmall.com/admin/product/baseTrademark/getTrademarkList base_trademark
    @ApiOperation("获取品牌属性")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> list = baseTrademarkService.list();
        return Result.ok(list);
    }

    //http://api.gmall.com/admin/product/saveSpuInfo spu_info spu_image  spu_sale_attr  spu_sale_attr_value
    @ApiOperation("添加spu")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SaveSpuInfoVo saveSkuInfoVo){
        spuInfoService.saveSpuInfo(saveSkuInfoVo);
        return Result.ok();
    }
}
